package com.zmj.sy.mom.srv.aps.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.Plate;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogApi;
import com.zmj.sy.mom.srv.aps.bean.vo.qms.*;
import com.zmj.sy.mom.srv.aps.feign.QmsApiClient;
import com.zmj.sy.mom.srv.aps.mapper.PlateMapper;
import com.zmj.sy.mom.srv.aps.mapper.SinoCamReceiveNestPartMapper;
import com.zmj.sy.mom.srv.aps.utils.JsonUtils;
import com.zmj.sy.mom.srv.aps.utils.LogApiConstant;
import com.zmj.sy.mom.srv.aps.utils.MkHttpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

@Slf4j
@RequiredArgsConstructor
@Service
public class ApsInformQmsService {

    private final MkHttpUtils mkHttpUtils;

    private final QmsApiClient qmsApiClient;
    private final PlateMapper plateMapper;
    private final SinoCamReceiveNestPartMapper sinoCamReceiveNestPartMapper;

    private BiConsumer<LogApi, String> consumer = (LogApi logApi, String body)->{
        JsonNode jsonNode = JsonUtils.readTree(body);
        if (jsonNode.get("code").intValue() == 0) {
            logApi.setSuccess(true);
        } else {
            logApi.setSuccess(false);
            logApi.setErrorMsg(jsonNode.get("message").asText());
        }
    };

    public void plateStartWork(String plateCode, String nestId, String phaseCode, String stationCode, String staffCode) {
        try {
            if (!phaseCode.equals("YC") && !phaseCode.equals("JG") && !phaseCode.equals("HG")) {
                return;
            }
            TaskPlateProcessReqVo reqVo = new TaskPlateProcessReqVo();
            reqVo.setPlateNo(plateCode);
            reqVo.setNestId(nestId);
            reqVo.setItemName("钢板");
            reqVo.setPhaseCode(phaseCode);
            reqVo.setStationCode(stationCode);
            reqVo.setCheckUser(staffCode);
            String constructNo = sinoCamReceiveNestPartMapper.findConstructNo(plateCode);
            reqVo.setConstructNo(constructNo);
            mkHttpUtils.postJson(LogApiConstant.QMS_SY, LogApiConstant.QMS_SY_PLATE_START, null, reqVo, consumer);
        } catch (Exception e) {
            log.info("调用QMS生成质检任务失败，钢板：{}，工序：{}，工位：{}，操作人：{}，时间：{}", plateCode, phaseCode, stationCode, staffCode, LocalDateTime.now());
        }
    }

    public void plateEndWork(String plateCode, String phaseCode) {
        try {
            if (!phaseCode.equals("YC") && !phaseCode.equals("JG") && !phaseCode.equals("HG")) {
                return;
            }

            PlateReqVo plateReqVo = new PlateReqVo();

            Plate one = plateMapper.lambdaQuery().eq(Plate::getMaterCode, plateCode).one();
            plateReqVo.setPlateNo(plateCode);
            plateReqVo.setNestId(one.getNestId());
            plateReqVo.setTexture(one.getTexture());
            plateReqVo.setThickness(Float.valueOf(one.getNorm()));

            List<PlatePartReqVo> partList = sinoCamReceiveNestPartMapper.getDataToQms(plateCode);
            plateReqVo.setPlateParts(partList);

            List<PlateReqVo> list = new ArrayList<>();
            list.add(plateReqVo);
            mkHttpUtils.postJson(LogApiConstant.QMS_SY, LogApiConstant.QMS_SY_PLATE_END, null, list, consumer);
        } catch (Exception e) {
            log.info("调用QMS创建零件明细失败，钢板：{}，时间：{}", plateCode, LocalDateTime.now());
        }
    }

    public void partStartWork(String plmId, String itemNo, String itemName, BigDecimal materialCount, String constructNo, String phaseCode, String stationCode, String staffCode, String palletNo, String stationNo, String orderCode, String packageNo) {
        try {
            TaskPartReqVo reqVo = new TaskPartReqVo();
            reqVo.setPlmId(plmId);
            reqVo.setItemNo(itemNo);
            reqVo.setItemName(itemName);
            reqVo.setMaterialCount(materialCount);
            reqVo.setConstructNo(constructNo);
            reqVo.setPhaseCode(phaseCode);
            reqVo.setStationCode(stationCode);
            reqVo.setCheckUser(staffCode);
            reqVo.setPalletNo(palletNo);
            reqVo.setStationNo(stationNo);
            reqVo.setOrderCode(orderCode);
            reqVo.setPackageNo(packageNo);
            mkHttpUtils.postJson(LogApiConstant.QMS_SY, LogApiConstant.QMS_SY_SMALL_PART_START, null, reqVo, consumer);
        } catch (Exception e) {
            log.info("调用QMS生成质检任务失败，物料号：{}，时间：{}", itemNo, LocalDateTime.now());
        }
    }

    public void assembleStartWork(String plmId, String itemNo, String itemName, BigDecimal materialCount, String constructNo, String phaseCode, String stationCode, String staffCode, String uniCode, String packageNo) {
        try {
            TaskAssemblyReqVo reqVo = new TaskAssemblyReqVo();
            reqVo.setCheckType("5");
            reqVo.setPlmId(plmId);
            reqVo.setUniqueCode(uniCode);
            reqVo.setItemNo(itemNo);
            reqVo.setItemName(itemName);
            reqVo.setMaterialCount(materialCount);
            reqVo.setConstructNo(constructNo);
            reqVo.setPhaseCode(phaseCode);
            reqVo.setStationCode(stationCode);
            reqVo.setCheckUser(staffCode);
            reqVo.setPackageNo(packageNo);
            mkHttpUtils.postJson(LogApiConstant.QMS_SY, LogApiConstant.QMS_SY_BIG_PART_START, null, reqVo, consumer);
        } catch (Exception e) {
            log.info("调用QMS生成质检任务失败，物料号：{}，唯一号：{}，时间：{}", itemNo, uniCode, LocalDateTime.now());
        }
    }

}
